<template>
  <el-container class="layout-container">
    <!-- 侧边�?-->
    <el-aside :width="isCollapse ? '80px' : '240px'" class="sidebar">
      <div class="logo">
        <el-icon v-if="isCollapse" class="logo-icon"><Box /></el-icon>
        <span v-else class="logo-text">WMS仓储系统</span>
      </div>
      
      <el-menu
        :default-active="activeMenu"
        :collapse="isCollapse"
        :unique-opened="true"
        router
        class="sidebar-menu"
      >
        <!-- 财务管理 -->
        <el-sub-menu index="financial">
          <template #title>
            <el-icon><Money /></el-icon>
            <span>财务管理</span>
          </template>
          <el-menu-item index="/financial/categories">财务类别</el-menu-item>
          <el-menu-item index="/financial/receivables">应收管理</el-menu-item>
          <el-menu-item index="/financial/payables">应付管理</el-menu-item>
          <el-menu-item index="/financial/accounting">财务记账</el-menu-item>
        </el-sub-menu>
        
        <!-- 仓库管理 -->
        <el-sub-menu index="warehouse">
          <template #title>
            <el-icon><OfficeBuilding /></el-icon>
            <span>仓库管理</span>
          </template>
          <el-sub-menu index="warehouse-inbound">
            <template #title>
              <span>入库管理</span>
            </template>
          </el-sub-menu>
          <el-sub-menu index="warehouse-outbound">
            <template #title>
              <span>出库管理</span>
            </template>
          </el-sub-menu>
          <el-sub-menu index="warehouse-transfer">
            <template #title>
              <span>移库管理</span>
            </template>
          </el-sub-menu>
          <el-sub-menu index="warehouse-damage">
            <template #title>
              <span>报损管理</span>
            </template>
          </el-sub-menu>
          <el-sub-menu index="warehouse-allocation">
            <template #title>
              <span>调拨管理</span>
            </template>
          </el-sub-menu>
          <el-sub-menu index="warehouse-count">
            <template #title>
              <span>盘点管理</span>
            </template>
          </el-sub-menu>
        </el-sub-menu>
        
        <!-- 业务管理 -->
        <el-sub-menu index="business">
          <template #title>
            <el-icon><Briefcase /></el-icon>
            <span>业务管理</span>
          </template>
          <el-sub-menu index="business-procurement">
            <template #title>
              <span>采购管理</span>
            </template>
          </el-sub-menu>
          <el-menu-item index="/business/returns">退货管理</el-menu-item>
          <el-sub-menu index="business-sales">
            <template #title>
              <span>销售管理</span>
            </template>
          </el-sub-menu>
          <el-menu-item index="/business/sales-returns">销售退货</el-menu-item>
        </el-sub-menu>
        
        <!-- 报表管理 -->
        <el-sub-menu index="reports">
          <template #title>
            <el-icon><DataAnalysis /></el-icon>
            <span>报表管理</span>
          </template>
          <el-menu-item index="/reports/inventory-list">库存清单</el-menu-item>
          <el-menu-item index="/reports/available-inventory">可出库存</el-menu-item>
          <el-menu-item index="/reports/period">期初期末</el-menu-item>
          <el-menu-item index="/reports/inventory-ledger">库存台账</el-menu-item>
          <el-menu-item index="/reports/inventory-warning">库存预警</el-menu-item>
          <el-menu-item index="/reports/inventory-capacity">库存容量</el-menu-item>
        </el-sub-menu>
        
        <!-- 基础资料 -->
        <el-sub-menu index="basic-data">
          <template #title>
            <el-icon><Document /></el-icon>
            <span>基础资料</span>
          </template>
          <el-sub-menu index="basic-warehouse">
            <template #title>
              <span>仓库管理</span>
            </template>
          </el-sub-menu>
          <el-sub-menu index="basic-location">
            <template #title>
              <span>库位管理</span>
            </template>
          </el-sub-menu>
          <el-menu-item index="/basic/equipment">设备管理</el-menu-item>
          <el-menu-item index="/basic/suppliers">供应商管理</el-menu-item>
          <el-menu-item index="/basic/customers">客户管理</el-menu-item>
          <el-menu-item index="/basic/units">单位管理</el-menu-item>
          <el-menu-item index="/basic/categories">产品类别</el-menu-item>
          <el-menu-item index="/basic/products">产品管理</el-menu-item>
          <el-menu-item index="/carrier/management">承运商管理</el-menu-item>
        </el-sub-menu>
        
        <!-- 系统设置 -->
        <el-sub-menu index="system">
          <template #title>
            <el-icon><Setting /></el-icon>
            <span>系统设置</span>
          </template>
          <el-sub-menu index="system-users">
            <template #title>
              <span>用户管理</span>
            </template>
          </el-sub-menu>
          <el-sub-menu index="system-roles">
            <template #title>
              <span>角色管理</span>
            </template>
          </el-sub-menu>
          <el-menu-item index="/system/departments">部门管理</el-menu-item>
          <el-menu-item index="/system/resources">资源管理</el-menu-item>
          <el-sub-menu index="system-messages">
            <template #title>
              <span>消息中心</span>
            </template>
            <el-menu-item index="/system/messages/details">消息详情</el-menu-item>
          </el-sub-menu>
        </el-sub-menu>
      </el-menu>
    </el-aside>
    
    <!-- 主内容区�?-->
    <el-container style="flex: 1; display: flex; flex-direction: column;">
      <!-- 顶部导航�?-->
      <el-header class="header">
        <div class="header-left">
          <el-button 
            type="text" 
            @click="toggleCollapse"
            class="collapse-btn"
          >
            <el-icon><Expand v-if="isCollapse" /><Fold v-else /></el-icon>
          </el-button>
          <el-breadcrumb separator="/" class="breadcrumb">
            <el-breadcrumb-item 
              v-for="item in breadcrumbList" 
              :key="item.path"
              :to="item.path"
            >
              {{ item.name }}
            </el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        
        <div class="header-right">
          <el-dropdown>
            <span class="user-info">
              <el-avatar :size="32" class="user-avatar">
                <el-icon><User /></el-icon>
              </el-avatar>
              <span class="username">{{ currentUser?.name || '管理' }}</span>
              <el-icon><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>个人设置</el-dropdown-item>
                <el-dropdown-item>修改密码</el-dropdown-item>
                <el-dropdown-item divided @click="handleLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <!-- 主内容区 -->
      <el-main class="main-content" style="flex: 1; padding: 0;">
        <div class="main-wrapper">
          <h2 class="page-title">{{ route.meta?.title || breadcrumbList[breadcrumbList.length - 1]?.name || '' }}</h2>
          <router-view />
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRoute } from 'vue-router'

const route = useRoute()
const isCollapse = ref(false)

// 当前激活的菜单
const activeMenu = computed(() => {
  return route.path
})

// 面包屑导航
const breadcrumbList = computed(() => {
  const pathArray = route.path.split('/').filter(item => item)
  const breadcrumbs = [{ name: '首页', path: '/dashboard' }]
  
  let currentPath = ''
  pathArray.forEach((item, index) => {
    currentPath += `/${item}`
    const name = getMenuName(item, index)
    if (name) {
      breadcrumbs.push({ name, path: currentPath })
    }
  })
  
  return breadcrumbs
})

// 获取菜单名称
const getMenuName = (path: string, index: number) => {
  const menuMap: Record<string, string> = {
    'financial': '财务管理',
    'financialCategories': '财务类别',
    'receivables': '应收管理',
    'payables': '应付管理',
    'accounting': '财务记账',
    'warehouse': '仓库管理',
    'inbound': '入库管理',
    'outbound': '出库管理',
    'transfer': '移库管理',
    'damage': '报损管理',
    'allocation': '调拨管理',
    'count': '盘点管理',
    'business': '业务管理',
    'procurement': '采购管理',
    'returns': '退货管理',
    'sales': '销售管理',
    'sales-returns': '销售退货',
    'reports': '报表管理',
    'inventory-list': '库存清单',
    'available-inventory': '可出库存',
    'period': '期初期末',
    'inventory-ledger': '库存台账',
    'inventory-warning': '库存预警',
    'inventory-capacity': '库存容量',
    'basic-data': '基础资料',
    'equipment': '设备管理',
    'suppliers': '供应商管理',
    'customers': '客户管理',
    'units': '单位管理',
    'categories': '产品类别',
    'products': '产品管理',
    'carriers': '承运商管理',
    'system': '系统设置',
    'users': '用户管理',
    'roles': '角色管理',
    'departments': '部门管理',
    'resources': '资源管理',
    'messages': '消息中心',
    'details': '消息详情'
  }
  
  return menuMap[path] || ''
}

// 切换侧边栏折叠状�?
const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

// 退出登录
const handleLogout = async () => {
  try {
    await ElMessageBox.confirm(
      '确定退出登录吗',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
    )
    
    userStore.logout()
    ElMessage.success('已退出登录')
    router.push('/login')
  } catch {
    // 用户取消退�?
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
  width: 100vw;
  display: flex;
  flex-direction: row;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

.sidebar {
  background: #ffffff;
  transition: width 0.3s;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
  border-right: 1px solid #e6e6e6;
}

.logo {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #2c3e50;
  font-size: 20px;
  font-weight: bold;
  border-bottom: 1px solid #e6e6e6;
  background: #f8f9fa;
  padding: 0 20px;
}

.logo-icon {
  font-size: 24px;
}

.logo-text {
  margin-left: 8px;
}

.sidebar-menu {
  border: none;
  background: transparent;
}

.sidebar-menu .el-menu-item,
.sidebar-menu .el-sub-menu__title {
  color: #606266;
  transition: all 0.3s ease;
  border-radius: 12px;
  margin: 6px 12px;
  width: calc(100% - 24px);
  padding: 0 16px;
  height: 48px;
  line-height: 48px;
}

.sidebar-menu .el-menu-item:hover,
.sidebar-menu .el-sub-menu__title:hover {
  background: linear-gradient(135deg, rgba(64, 158, 255, 0.1) 0%, rgba(64, 158, 255, 0.05) 100%);
  color: #409eff;
  transform: translateX(4px);
}

.sidebar-menu .el-menu-item.is-active {
  background: linear-gradient(135deg, #409eff 0%, #66b1ff 100%);
  color: #fff;
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.sidebar-menu .el-sub-menu .el-menu-item {
  margin: 2px 16px;
  width: calc(100% - 32px);
  color: #909399;
}

.sidebar-menu .el-sub-menu .el-menu-item:hover {
  color: #409eff;
}

.sidebar-menu .el-sub-menu .el-menu-item.is-active {
  color: #409eff;
  background: rgba(64, 158, 255, 0.1);
}

.header {
  background: linear-gradient(135deg, #fff 0%, #f8f9fa 100%);
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.header-left {
  display: flex;
  align-items: center;
}

.collapse-btn {
  margin-right: 20px;
  font-size: 18px;
}

.breadcrumb {
  font-size: 14px;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 8px 16px;
  border-radius: 20px;
  transition: all 0.3s ease;
  background: linear-gradient(135deg, rgba(64, 158, 255, 0.1) 0%, rgba(64, 158, 255, 0.05) 100%);
  border: 1px solid rgba(64, 158, 255, 0.2);
}

.user-info:hover {
  background: linear-gradient(135deg, rgba(64, 158, 255, 0.2) 0%, rgba(64, 158, 255, 0.1) 100%);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
}

.user-avatar {
  margin-right: 8px;
}

.username {
  margin-right: 8px;
  font-size: 14px;
}

.main-content {
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 0;
  overflow-y: auto;
  overflow-x: hidden;
  height: 100vh;
  flex: 1;
}

.main-wrapper {
  height: 100%;
  width: 100%;
  padding: 32px 40px;
  background: transparent;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
}

.page-title {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 12px;
  color: #2c3e50;
}
</style>


